package com.EAsystem.service.Impl;

import com.EAsystem.entity.Student;
import com.EAsystem.mapper.StudentMapper;
import com.EAsystem.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 学生服务实现类
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");



    @Override
    public PageInfo<Student> findAllByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> students = studentMapper.findAllStudentsWithPage();
        return new PageInfo<>(students);
    }

    @Override
    public Student findById(Integer id) {
        return studentMapper.findStudentById(id);
    }

    @Override
    public Student findByUserId(Integer userId) {
        return studentMapper.findStudentByUserId(Long.valueOf(userId));
    }

    @Override
    public Student findByStudentNo(String studentNo) {
        return studentMapper.findStudentByStudentNo(studentNo);
    }

    @Override
    public List<Student> findByClassId(Integer classId) {
        return studentMapper.findStudentsByClassId(classId);
    }


    @Override
    public PageInfo<Student> findByCondition(Student student, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> students = studentMapper.findStudentsByCondition(student);
        return new PageInfo<>(students);
    }

    @Override
    @Transactional
    public boolean add(Student student) {
        // 设置创建时间和更新时间
        String currentTime = sdf.format(new Date());
        student.setCreateTime(currentTime);
        student.setUpdateTime(currentTime);
        
        return studentMapper.insertStudent(student) > 0;
    }

    @Override
    @Transactional
    public boolean update(Student student) {
        // 设置更新时间
        student.setUpdateTime(sdf.format(new Date()));
        
        return studentMapper.updateStudent(student) > 0;
    }

    @Override
    @Transactional
    public boolean delete(Integer id) {
        return studentMapper.deleteStudentById(id) > 0;
    }




}